#include "setting.h"

#include "user.h"
#include "db.h"
#include "global.h"

#include <qjsonobject.h>

Setting::Setting(QObject *parent)
    : QObject{parent}
{}

QHash<QString, QVariant> Setting::getSettingParams()
{
    DB* db = DB::getInstance();
    return db->getSettingParams();
}

bool Setting::saveSettingParam(const QString &param, const QString &value)
{
    User* pUser = (User*)Global::instance().get("userInfo");
    QString loginName = pUser->currentLogin()["name"].toString();
    QString realName = pUser->currentLogin()["real_name"].toString();

    DB* db = DB::getInstance();
    db->saveParamValue(param,value,loginName);

    QJsonObject jsonObj;
    jsonObj["param"]   = param;
    jsonObj["value"] = value;                  // 整数
    jsonObj["name"] = loginName;              // 字符串

    QJsonDocument jd(jsonObj);
    DB::getInstance()->addLog(loginName, realName, "修改参数", param+"->"+value, jd.toJson(QJsonDocument::Indented), loginName);
    if(param.compare("system_dataSamplingPeriod") == 0 || param.compare("system_decimalPlaces")==0){
        emit signalUpdateParam(param, value);
    }
    return true;
}

QString Setting::getSettingParam(const QString &param)
{
    DB* db = DB::getInstance();
    return db->getSettingParam(param);
}
